<template>
  <div class="remittance-wrap">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      :inline="true"
      label-width="150px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司名称" prop="companyName" class="form-item">
            <el-input
              v-model="formData.companyName"
              placeholder="请输入公司名称"
              clearable
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="收款账户"
            prop="harvestAccountMark"
            class="form-item"
          >
            <el-select
              clearable
              v-model="formData.harvestAccountMark"
              placeholder="请选择收款账户"
            >
              <el-option
                v-for="item in bus_harvest_account"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="汇款名称" prop="remitName" class="form-item">
            <el-input
              v-model="formData.remitName"
              placeholder="请输入汇款名称"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="汇款金额" prop="remitPrice" class="form-item">
            <el-input-number
              v-model="formData.remitPrice"
              placeholder="请输入汇款金额"
              :precision="2"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="汇款币种" prop="currencyType" class="form-item">
            <el-select
              clearable
              v-model="formData.currencyType"
              @change="currencyChange"
              placeholder="请选择汇款币种"
            >
              <el-option
                v-for="item in bus_currency_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="营业执照附件" prop="remitImg" class="form-item">
            <ImageUpload v-model="formData.remitImg" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item
            label="折合钱包充值金额"
            prop="rechargePrice"
            class="form-item"
          >
            <el-input
              v-model="formData.rechargePrice"
              placeholder="请输入折合钱包充值金额"
              clearable
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="钱包币种" prop="walletCurType" class="form-item">
            <el-select
              clearable
              disabled
              v-model="formData.walletCurType"
              placeholder="请选择汇款币种"
            >
              <el-option
                v-for="item in bus_currency_type"
                :key="item.value"
                :label="item.elTagClass"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-button type="primary" @click="handleSubmit">确认提交汇款记录</el-button>
  </div>
</template>

<script setup>
import { ref, reactive, watch, onMounted } from "vue";

import {
  calculateLocalMoney,
  customerRecharge,
} from "@/api/customer_handle/index.js";
const { proxy } = getCurrentInstance();
const { bus_harvest_account, bus_currency_type } = proxy.useDict(
  "bus_harvest_account",
  "bus_currency_type"
);

const props = defineProps({
  userInfo: {
    type: Object,
    default: () => {},
  },
});

const formRef = ref();

const formData = reactive({
  companyName: "",
  harvestAccountMark: "",
  remitName: "",
  remitImg: "",
  remitPrice: "",
  rechargePrice: "",
  walletCurType: "",
});

const formRules = reactive({
  harvestAccountMark: [
    { required: true, message: "请选择收款账户", trigger: "change" },
  ],
  remitName: [{ required: true, message: "请输入汇款名称", trigger: "blur" }],
  remitPrice: [{ required: true, message: "请输入汇款金额", trigger: "blur" }],
  remitImg: [{ required: true, message: "请上传汇款截图", trigger: "blur" }],
});

function handleSubmit() {
  proxy.$refs.formRef.validate((valid) => {
    if (valid) {
      const params = {
        harvestAccountMark: formData.harvestAccountMark,
        remitName: formData.remitName,
        remitPrice: formData.remitPrice,
        currencyType: formData.currencyType,
        remitImg: formData.remitImg,
        rechargePrice: formData.rechargePrice,
      };
      customerRecharge(params).then(({ data }) => {
        resetForm();
        proxy.$message.success("提交成功");
      });
    }
  });
}

function calculateMoney() {
  if (!formData.currencyType || !formData.remitPrice) {
    return;
  }
  calculateLocalMoney({
    localCurrencyType: formData.currencyType,
    paymentPrice: formData.remitPrice,
  }).then(({ data }) => {
    formData.rechargePrice = data.price;
  });
}

function currencyChange() {
  calculateMoney();
}

onMounted(() => {
  console.log(props.userInfo);

  //   getUserInfo().then(({ data }) => {
  //     formData.companyName = data.customerName;
  //     formData.walletCurType = data.currencyType;
  //     // formData.companyName = res.data.companyName;
  //   });
});

function resetForm() {
  Object.assign(formData, {
    companyName: "",
    harvestAccountMark: "",
    remitName: "",
    remitImg: "",
    remitPrice: "",
    rechargePrice: "",
    walletCurType: "",
  });
  formRef.value.resetFields();
}

watch(
  () => formData.remitPrice,
  () => {
    calculateMoney();
  }
);
watch(
  () => props.userInfo,
  (val) => {
    if (val) {
      formData.companyName = val.customerName;
      formData.walletCurType = val.currencyType;
    }
  }
);
</script>

<style scoped lang="scss">
.remittance-wrap {
  margin-top: 20px;
}
.form-item {
  width: 100%;
}
</style>
